package cn.edu.cqie.controller;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.edu.cqie.bo.AccountBo;
import cn.edu.cqie.config.ResultBean;
import cn.edu.cqie.dto.AccountRequestDto;
import cn.edu.cqie.enums.RespToAppEnum;
import cn.edu.cqie.service.AccountService;
import cn.edu.cqie.utils.BizException;
import cn.edu.cqie.utils.LoginVerify;

/**
 * @author potato 账号入驻
 */
@RestController
@RequestMapping("/account")
public class AccountController {

	private final static Logger logger = LoggerFactory.getLogger(AccountController.class);

	@Autowired
	private AccountService accountService;

	@PostMapping("/load")
	@ResponseBody
	public ResultBean load(@RequestBody AccountRequestDto dto) {

		logger.info("account-load-AccountRequest,{}", JSONObject.toJSONString(dto));

		Map<String, Object> result = new HashMap<String, Object>();

		// 1登录校验，后期可以放到切面中实现
		LoginVerify.checkLogin(dto.getToken());

		// 2封装业务逻辑对象bo
		AccountBo bo = new AccountBo();
		BeanUtils.copyProperties(dto, bo);

		// 3业务逻辑处理
		accountService.load(bo);

		// 4.封装返回值
		result.put("gameAreaLst", bo.getGameAreaLst());
		result.put("gameServerLst", bo.getGameServerLst());
		result.put("gameCampLst", bo.getGameCampLst());
		result.put("gameSectLst", bo.getGameSectLst());
		result.put("gamebodyTypeLst", bo.getGamebodyTypeLst());
		result.put("gameEquipmentLst", bo.getGameEquipmentLst());
		result.put("gameAccountHighlightsLst", bo.getGameAccountHighlightsLst());

		logger.info("account-load-AccountResp,{}", JSONObject.toJSONString(result));

		return ResultBean.success(result);
	}

	@PostMapping("/add")
	@ResponseBody
	public ResultBean add(@RequestBody @Valid AccountRequestDto dto, BindingResult bindingResult) {

		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("account-add-AccountRequest,{}", JSONObject.toJSONString(dto));

		// 1.参数校验
		if (bindingResult.hasErrors()) {
			String message = bindingResult.getFieldError().getDefaultMessage();

			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage() + ":" + message);
			return ResultBean.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(),
					message);
		}

		// 2封装业务逻辑对象bo
		AccountBo bo = new AccountBo();
		BeanUtils.copyProperties(dto, bo);

		// 3业务逻辑处理
		accountService.add(bo);

		logger.info("account-add-AccountResp,{}", JSONObject.toJSONString(result));

		return ResultBean.success(result);
	}

	@PostMapping("/addPicture")
	@ResponseBody
	public ResultBean addPicture(@RequestBody AccountRequestDto dto) {

		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("account-addPicture-AccountRequest,{}", JSONObject.toJSONString(dto));

		// 1.参数校验
		appPictureVerify(dto);

		// 2封装业务逻辑对象bo
		AccountBo bo = new AccountBo();
		BeanUtils.copyProperties(dto, bo);

		// 3业务逻辑处理
		accountService.addPicture(bo);

		// 4组装返回值
		result.put("imgId", bo.getImgId());
		result.put("imgUrl", bo.getImgUrl());

		logger.info("account-addPicture-AccountResp,{}", JSONObject.toJSONString(result));

		return ResultBean.success(result);
	}

	@PostMapping("/update")
	@ResponseBody
	public ResultBean update(@RequestBody @Valid AccountRequestDto dto, BindingResult bindingResult) {

		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("account-update-AccountRequest,{}", JSONObject.toJSONString(dto));

		// 1.参数校验
		if (bindingResult.hasErrors()) {
			String message = bindingResult.getFieldError().getDefaultMessage();

			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage() + ":" + message);
			return ResultBean.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(),
					message);
		}

		// 2封装业务逻辑对象bo
		AccountBo bo = new AccountBo();
		BeanUtils.copyProperties(dto, bo);

		// 3业务逻辑处理
		accountService.update(bo);

		logger.info("account-update-AccountResp,{}", JSONObject.toJSONString(result));

		return ResultBean.success(result);
	}

	@PostMapping("/confirm")
	@ResponseBody
	public ResultBean confirm(@RequestBody AccountRequestDto dto) {

		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("account-confirm-AccountRequest,{}", JSONObject.toJSONString(dto));

		// 1.参数校验
		confirmVerify(dto);

		// 2封装业务逻辑对象bo productId/checkState/state/coment
		AccountBo bo = new AccountBo();
		BeanUtils.copyProperties(dto, bo);

		// 3业务逻辑处理
		accountService.confirm(bo);

		logger.info("account-confirm-AccountResp,{}", JSONObject.toJSONString(result));

		return ResultBean.success(result);
	}

	private void appPictureVerify(AccountRequestDto dto) {
		// TODO Auto-generated method stub
		if (dto.getFile().isEmpty()) {
			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(), "File不能为空");
			throw new BizException(RespToAppEnum.BADREQUEST.getCode(),
					RespToAppEnum.BADREQUEST.getMessage() + "File不能为空");
		}
		if (!StringUtils.isNumeric(dto.getImgType())) {
			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(), "imageType格式不正确");
			throw new BizException(RespToAppEnum.BADREQUEST.getCode(),
					RespToAppEnum.BADREQUEST.getMessage() + "imageType格式不正确");
		}
		if (!StringUtils.isNumeric(dto.getProductId())) {
			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(), "productId格式不正确");
			throw new BizException(RespToAppEnum.BADREQUEST.getCode(),
					RespToAppEnum.BADREQUEST.getMessage() + "productId格式不正确");
		}
	}

	private void confirmVerify(AccountRequestDto dto) {
		if (!StringUtils.isNumeric(dto.getCheckState())) {
			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(), "checkState格式不正确");
			throw new BizException(RespToAppEnum.BADREQUEST.getCode(),
					RespToAppEnum.BADREQUEST.getMessage() + "checkState格式不正确");
		}
		if (!StringUtils.isNumeric(dto.getProductId())) {
			logger.error(RespToAppEnum.BADREQUEST.getCode() + RespToAppEnum.BADREQUEST.getMessage(), "productId格式不正确");
			throw new BizException(RespToAppEnum.BADREQUEST.getCode(),
					RespToAppEnum.BADREQUEST.getMessage() + "productId格式不正确");
		}
	}

}
